package classes;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailConfirmation 
{
	private String recipient;
	private final String sender = "totalitea.noreply@gmail.com";
	private final String password = "totalitea123";
	private final String host = "smtp.gmail.com";
	
	public void sendEmailConformation()
	{
		try
		{
			Properties properties = System.getProperties();
			
			// Setup GoogleMail server
		    properties.setProperty("mail.smtp.host", host);			// GoogleMail smtp host
		    properties.put("mail.smtp.auth", "true");				// Authorisation is needed to send email
		    properties.put("mail.smtp.starttls.enable", "true");	// TLS protocol is being used 
		    properties.put("mail.smtp.port", "587");
		    
		    System.out.println("Created smtp server");
		    
		    // Get the default Session object.
		    Session session = Session.getDefaultInstance(properties, 
		    		new Authenticator()
		    		{ 
		    			protected PasswordAuthentication getPasswordAuthentication()
		    			{
		    				return new PasswordAuthentication(sender, password);
		    			}
		    		});
		    
		    System.out.println("Authentication successful");
		    
		    Message message = new MimeMessage(session);
		    
		    message.setFrom(new InternetAddress(sender));
			message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient));
		    message.setSubject("Thank you for your order from Totalitea!");
		    String msg="This is just an email to confirm that your order has been received.";
		    message.setContent(msg, "text/html; charset=utf-8");
		    Transport.send(message);
		    
		    System.out.println("Email sent successfully");
		}
		catch (AddressException e) 
	    {
	    	e.printStackTrace();
		} 
	    catch (MessagingException e) 
	    {
			e.printStackTrace();
		}
	    
	}
	
	public String getRecipient() 
	{
		return recipient;
	}

	public void setRecipient(String recipient) 
	{
		this.recipient = recipient;
	}
	
}
